package org.linlinjava.litemall.core.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Bean工具类
 *
 * @author 聂鑫 niexin@passiontec.cn
 * @since 2018-01-03 16:12
 **/
public class BeanUtil{
    public static <T> T transformBean(Object source, Class<T> clazz) {
        T target = BeanUtils.instantiate(clazz);
        BeanUtils.copyProperties(source, target);
        return target;
    }

    public static <T> List<T> transformListBean(List list, Class<T> clazz) {
        List<T> target = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            T tar;
            for(Object t : list){
                tar = transformBean(t, clazz);
                target.add(tar);
            }
        }
        return target;
    }
}
